package com.hgwz.api;

import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/")
public class RestfulAPI {

    @GetMapping("/")
    public String sayHello(){
        String msg = "Hi, this is a springboot demo!";
        System.out.println(msg);
        return msg;
    }

    @GetMapping("/params")
    public String params(@RequestParam(required = true) String key1,
                         @RequestParam(required = false) String key2,
                         @RequestParam(required = false, defaultValue = "ThisIsDefaultValue") String key3) {
        String rt = "Key1 = " + key1 + "\nKey2 = " + key2 + "\nKey3 = " + key3;
        System.out.println(rt);
        return rt;
    }

    @GetMapping("/pathvariable/{id1}/{id2}/no")
    public String pathVariable( @PathVariable("id1") String id1
            , @PathVariable("id2") String id2) {

        String rt = "id1 = " + id1 + "\nid2 = " + id2;
        System.out.println(rt);
        return rt;
    }

    @PostMapping("/post")
    public String helloPost(@RequestBody @Valid ReqBody requestBody){
        String rt = "param1 = " + requestBody.getParam1() + "\nparam2 = " + requestBody.getParam2();
        System.out.println(rt);
        return rt;
    }

    @DeleteMapping("/delete")
    public String helloDelete(@RequestParam(required = true) String name){
        String rt = "The name " + name + " has been deleted!";
        System.out.println(rt);
        return rt;
    }

    @GetMapping("/test")
    public String helloError(){
        String rt = "Anx error api";
        System.out.println(rt);
        int i = 3/0;
        return rt;
    }
}
